<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>{{ title }}</span>
      </div>
      <!-- 添加或修改慢病中心-医院对话框 -->
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="医院名称" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入医院名称"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="医院地址" prop="address">
              <el-input
                v-model="form.address"
                placeholder="请输入医院地址"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="咨询热线" prop="consultationHotline">
              <el-input
                v-model="form.consultationHotline"
                placeholder="请输入咨询热线"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="微信号" prop="weChat">
              <el-input v-model="form.weChat" placeholder="请输入微信号" clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="小程序二维码" prop="picture">
              <image-upload v-model="form.picture" :limit="1" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="公众号二维码" prop="commonQrCode">
              <image-upload v-model="form.commonQrCode" :limit="1" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="客服二维码" prop="customerQrCode">
              <image-upload v-model="form.customerQrCode" :limit="1" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="底部二维码" prop="miniProgramQrCode">
              <image-upload v-model="form.miniProgramQrCode" :limit="1" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="医院介绍">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
      </el-form>

      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import {
  listHospital,
  getHospital,
  delHospital,
  addHospital,
  updateHospital,
} from "@/api/chronicDisease/hospital";

export default {
  name: "Hospital",
  dicts: ["yy_content_status"],
  data() {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "医院名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    if (this.$route.query.id) {
      // 有id就修改
      this.id = this.$route.query.id;
      this.getHospitalInfo(this.$route.query.id); //获取表单数据
    } else {
      this.reset();
    }
    this.title = this.$route.query.metaTitle;
  },
  activated() {
    if (this.$route.query.id && this.$route.query.id != this.id) {
      // 有id就修改
      this.getHospitalInfo(this.$route.query.id); //获取表单数据
    }
  },
  methods: {
    // 取消按钮
    cancel() {
      this.reset();
      // 关闭当前tabs
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.replace({
        path: "/chronicDisease/hospital",
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        address: null,
        consultationHotline: null,
        content: null,
        picture: null,
        status: null,
        weChat: null,
        commonQrCode: null,
        customerQrCode: null,
        miniProgramQrCode: null,
        createBy: null,
        delFlag: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    getHospitalInfo(id) {
      this.reset();
      getHospital(id).then((response) => {
        this.form = response.data;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateHospital(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              // 关闭当前tabs
              this.$store.dispatch("tagsView/delView", this.$route);
              this.$router.replace({
                path: "/chronicDisease/hospital",
              });
            });
          } else {
            addHospital(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              // 关闭当前tabs
              this.$store.dispatch("tagsView/delView", this.$route);
              this.$router.replace({
                path: "/chronicDisease/hospital",
              });
            });
          }
        }
      });
    },
  },
};
</script>
<style scoped>
.dialog-footer {
  text-align: right;
}
</style>
